package com.kidback.bbloc259.ui.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.CameraUpdateFactory;
import com.kidback.bbloc259.api.BBNetApi;
import com.kidback.bbloc259.app.APPConst;
import com.kidback.bbloc259.base.BaseActivity;
import com.kidback.bbloc259.bean.LocationBean;
import com.kidback.bbloc259.databinding.ActivityViewBinding;
import com.kidback.bbloc259.databinding.ItemAddressBinding;
import com.kidback.bbloc259.network.base.ApiHelper;
import com.kidback.bbloc259.network.beans.BaseResponse;
import com.kidback.bbloc259.network.errorhandler.ResponseThrowable;
import com.kidback.bbloc259.network.observer.BaseObserver;
import com.kidback.bbloc259.util.CollectionUtils;
import com.kidback.bbloc259.util.LogUtils;
import com.kidback.bbloc259.util.StatusBarUtils;
import com.kidback.bbloc259.util.TimeUtils;
import com.kidback.bbloc259.util.TipUtils;

import java.util.Collections;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class ViewActivity extends BaseActivity<ActivityViewBinding> {

    private static String userId;
    private static String nickName;

    private UserViewMapLayer userViewMapLayer;

    public static void launch(Context context, String userId, String nickName) {
        Intent intent = new Intent(context, ViewActivity.class);
        intent.putExtra("userId", userId);
        intent.putExtra("nickName", nickName);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        context.startActivity(intent);
    }

    @Override
    public ActivityViewBinding getViewBinding() {
        return ActivityViewBinding.inflate(inflater);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding.mapView.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        userViewMapLayer.onDestroy();
    }

    @Override
    protected void initView() {

        userViewMapLayer = new UserViewMapLayer(this, viewBinding.mapView);

        userId = getIntent().getStringExtra("userId");
        nickName = getIntent().getStringExtra("nickName");

        StatusBarUtils.setStatusBarColor(activity, 0xff715af3);

        viewBinding.titleBar.setTintColor(Color.WHITE);
        viewBinding.titleBar.setTitle(nickName);

        viewBinding.titleBar.setOnBackClickListener(onBackClickListener);

        viewBinding.mapView.getMap().getUiSettings().setRotateGesturesEnabled(false);
        viewBinding.mapView.getMap().getUiSettings().setMyLocationButtonEnabled(false);

        viewBinding.mapView.getMap().getUiSettings().setMyLocationButtonEnabled(false);
        viewBinding.mapView.getMap().setMyLocationEnabled(false);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
        viewBinding.mapView.getMap().getUiSettings().setZoomPosition(AMapOptions.ZOOM_POSITION_RIGHT_CENTER);
        viewBinding.mapView.getMap().getUiSettings().setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_LEFT);

        viewBinding.mapView.getMap().moveCamera(CameraUpdateFactory.zoomTo(APPConst.MAP_ZOOM_DEFAULT));

        viewBinding.recyclerView.setLayoutManager(new LinearLayoutManager(activity));
    }

    @Override
    protected void loadData() {

        Observable<BaseResponse<List<LocationBean>>> observable = BBNetApi.getBBService().getLocationList(userId, "-");
        Observer<BaseResponse<List<LocationBean>>> observer = new BaseObserver<BaseResponse<List<LocationBean>>>() {

            @Override
            public void onSubscribe(Disposable d) {
                super.onSubscribe(d);
            }

            @Override
            public void onSuccess(BaseResponse<List<LocationBean>> response) {
                TipUtils.hideLoadingFixDialog();

                AddressAdapter adapter = new AddressAdapter(response.data);
                viewBinding.recyclerView.setAdapter(adapter);

                //userViewMapLayer.drawRoute(response.data);
                //TipUtils.showMsgDuration(activity, "查询成功", Gravity.CENTER, 1000);
            }

            @Override
            public void onFailure(ResponseThrowable e) {
                TipUtils.hideLoadingFixDialog();
                TipUtils.showMsgFixed(activity, e.getMessage());
            }
        };

        ApiHelper.request(observable, observer);

        TipUtils.showLoadingFixDialog(activity, "加载中");

    }

    private class AddressVH extends RecyclerView.ViewHolder{

        public ItemAddressBinding binding;

        public AddressVH(ItemAddressBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    private class AddressAdapter extends RecyclerView.Adapter<AddressVH>{

        private List<LocationBean> dataList;

        public AddressAdapter(List<LocationBean> dataList) {
            Collections.reverse(dataList);
            this.dataList = dataList;
        }

        @NonNull
        @Override
        public AddressVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ItemAddressBinding binding = ItemAddressBinding.inflate(LayoutInflater.from(activity), parent, false);
            AddressVH vh = new AddressVH(binding);

            return vh;
        }

        @Override
        public void onBindViewHolder(@NonNull AddressVH holder, int position) {

            holder.binding.tvTime.setText(CollectionUtils.get(dataList, position).create_time);
            holder.binding.tvLatitude.setText(String.valueOf(CollectionUtils.get(dataList, position).latitude));
            holder.binding.tvLongitude.setText(String.valueOf(CollectionUtils.get(dataList, position).longitude));
            holder.binding.tvAddress.setText(String.valueOf(CollectionUtils.get(dataList, position).address));

        }

        @Override
        public int getItemCount() {
            return CollectionUtils.size(dataList);
        }

    }


}
